package com.lzb.randomturntable.adapter;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;

import androidx.annotation.Keep;

import com.lzb.randomturntable.bean.TurnTableItem;
import com.lzb.randomturntable.view.TurnTableView;

/**
 * Desc: 文字沿着从圆边向圆心的路径而绘制的adapter
 */
@Keep
public class TextLineToCenterAdapter extends TurnTableView.Adapter<TurnTableItem> {

    private float textHOffset = 0;

    public void setTextHOffset(float textHOffset) {
        this.textHOffset = textHOffset;
    }

    @Override
    protected void onDrawItem(Canvas canvas, RectF oval, float startAngle, float sweepAngle, Paint paint, int itemPos) {
        TurnTableItem item = getItem(itemPos);
        //bg
        paint.setColor(item.bgColor);
        canvas.drawArc(oval, startAngle, sweepAngle, true, paint);

        //text
        if (item.textColor != 0) {
            paint.setColor(item.textColor);
        }
        if (item.textSize > 0) {
            paint.setTextSize(item.textSize);
        }

        float radius = oval.width() / 2;
        float centerX = oval.centerX();
        float centerY = oval.centerY();
        float pointX = 0;
        float pointY = 0;

        float angle = startAngle + 90 + sweepAngle / 2;
        int intAngle = (int) angle;
        float calAngle = angle;
        if (intAngle == 0) {
            pointX = centerX;
            pointY = centerY - radius;
        } else if (intAngle < 90) {
            calAngle = (float) Math.toRadians(90 - angle);
            pointX = (float) (centerX + radius * Math.cos(calAngle));
            pointY = (float) (centerY - radius * Math.sin(calAngle));
        } else if (intAngle == 90) {
            pointX = centerX + radius;
            pointY = centerY;
        } else if (intAngle < 180) {
            calAngle = (float) Math.toRadians(angle - 90);
            pointX = (float) (centerX + radius * Math.cos(calAngle));
            pointY = (float) (centerY + radius * Math.sin(calAngle));
        } else if (intAngle == 180) {
            pointX = centerX;
            pointY = centerY + radius;
        } else if (intAngle < 270) {
            calAngle = (float) Math.toRadians(270 - angle);
            pointX = (float) (centerX - radius * Math.cos(calAngle));
            pointY = (float) (centerY + radius * Math.sin(calAngle));
        } else if (intAngle == 270) {
            pointX = centerX - radius;
            pointY = centerY;
        } else if (intAngle < 360) {
            calAngle = (float) Math.toRadians(360 - angle);
            pointX = (float) (centerX - radius * Math.sin(calAngle));
            pointY = (float) (centerY - radius * Math.cos(calAngle));
        } else {
            pointX = centerX;
            pointY = centerY - radius;
        }

        Path path = new Path();
        path.moveTo(pointX, pointY);
        path.lineTo(centerX, centerY);

        float textWidth = paint.measureText(item.text);
        float hOffset = (radius - textWidth) / 2 / 2 + textHOffset;
        canvas.drawTextOnPath(item.text, path, hOffset, paint.getTextSize() / 2, paint);
    }
}
